package com.example.material

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bumptech.glide.Glide
import com.example.material.databinding.ActivitySwipeRefreshLayoutBinding

class SwipeRefreshLayoutActivity : AppCompatActivity() {
    val images = mutableListOf(
        R.drawable.apple,
        R.drawable.banana,
        R.drawable.orange,
        R.drawable.watermelon,
        R.drawable.pear,
        R.drawable.grape,
        R.drawable.pineapple,
    )

    private lateinit var binding: ActivitySwipeRefreshLayoutBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivitySwipeRefreshLayoutBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        /**
         * swipeRefreshLayout
         */
        binding.swipeRefresh.setColorSchemeResources(R.color.teal_700)
        binding.swipeRefresh.setOnRefreshListener {
            val index = (0 until images.size).random()
            val image = images[index]
            Glide.with(this).load(image).into(binding.imageView)

            binding.swipeRefresh.isRefreshing = false
        }
    }
}